package org.kms.Controllers;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.kms.dao.childDao;
import org.kms.dao.kqinfoDao;
import org.kms.pojo.Child;
import org.kms.pojo.Kqinfomanager;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/KqInfo")
@CrossOrigin(value = "*")
public class kqInfo {
    @Autowired
    private kqinfoDao kqinfoDao;

    @Autowired
    private childDao childDao;

    @RequestMapping("/Index")
    @ResponseBody
    public ResponseResult<List<Kqinfomanager>> getall(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Kqinfomanager condition,Integer cmId,Integer gradeId,Integer glId,String sign,String b)throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowdate = sdf.format(new Date());
        List<Kqinfomanager> kqinfomanagers =kqinfoDao.getbyDateNow(nowdate);
       if (kqinfomanagers.size() == 0 ){
           List<Child> childList =childDao.getbyglId(glId);
           if (childList.size() >0){
               for (Child c:childList
               ) {
                   Kqinfomanager kqinfomanager = new Kqinfomanager();
                   kqinfomanager.setPid(c.getPid());
                   kqinfomanager.setGlId(glId);
                   kqinfomanager.setSign(nowdate);
                   kqinfoDao.add(kqinfomanager);
               }
           }
       }
        Page<Kqinfomanager> pager = PageHelper.startPage(page,limit);

       condition.setGlId(glId);
       Child child = new Child();
       child.setGradeId(gradeId);
       child.setCmId(cmId);
       condition.setChild(child);
       condition.setSign(sign);
       condition.setB(b);
        System.out.println(condition.getSign());
        System.out.println(condition.getB());
       if (condition.getSign() == null && condition.getB() == null){
           condition.setSign(nowdate);
           condition.setB("");
       }
       /* System.out.println(condition.getB());
        System.out.println(condition.getSign());*/
        List<Kqinfomanager> kqinfomanagerList =kqinfoDao.getConditionList(condition);
        return new ResponseResult<>(kqinfomanagerList,pager.getTotal());
    }


    @RequestMapping("/Index2")
    @ResponseBody
    public ResponseResult<List<Kqinfomanager>> getall2(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Kqinfomanager condition,Integer cmId,Integer gradeId,Integer glId,String sign,String b)throws Exception{
        Page<Kqinfomanager> pager = PageHelper.startPage(page,limit);
        condition.setSign(sign);
        condition.setB(b);
        condition.setGlId(glId);
        Child child = new Child();
        child.setGradeId(gradeId);
        child.setCmId(cmId);
        condition.setChild(child);
        List<Kqinfomanager> kqinfomanagerList =kqinfoDao.getConditionList(condition);
        return new ResponseResult<>(kqinfomanagerList,pager.getTotal());
    }

    @RequestMapping("/updDid")
    @ResponseBody
    public ResponseResult updDid(Kqinfomanager kqinfomanager)throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        kqinfomanager.setSign(sdf.format(new Date()));
        kqinfoDao.update(kqinfomanager);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/updDid2")
    @ResponseBody
    public ResponseResult updDid2(Kqinfomanager kqinfomanager,String cmidaaa)throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        kqinfomanager.setSign(sdf.format(new Date()));
        kqinfomanager.setDid(22);
        String []a= cmidaaa.split(",");
        for (int i = 0; i < a.length; i++) {
            kqinfomanager.setaMid(Integer.parseInt(a[i]));
            kqinfoDao.update(kqinfomanager);
        }

        return ResponseResult.createSuccessResult();
    }
    @InitBinder
    public void InitBinder(WebDataBinder webDataBinder) throws Exception{
        //日期类型转换器
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat,true);
        //注册类型换换器:如果碰到Date.class 都会转成yyyy-MM-dd格式
        webDataBinder.registerCustomEditor(Date.class,dateEditor);
    }

}
